<template>
	<view class="bill-page">
		<detail-head url="../mine/mine" type="none"></detail-head>

		<view class="list-type">
			<view :class="['type', active===1 ? 'active' : '' ]" @click="onTypeChange(1)">全部</view>
			<view :class="['type', active===2 ? 'active' : '' ]" @click="onTypeChange(2)">进行中</view>
			<view :class="['type', active===3 ? 'active' : '' ]" @click="onTypeChange(3)">已完成</view>
		</view>

		<view class="list" v-show="active === 1">
			<view class="item" v-for="item in all" :key="item.id">
				<view class="title">"{{item.activityProductVO ? item.activityProductVO.name : ''}}"</view>
				<view class="right">
					<text v-if="item.payStatus === 'PAYMENTS'">支付中</text>
					<text v-if="item.payStatus === 'CANCEL'">支付取消</text>
					<text v-if="item.payStatus === 'SUCCESS'">支付成功</text>
					<text v-if="item.payStatus === 'FAIL'">支付失败</text>
				</view>
				<view class="infos">
					<view class="info-img">
						<image :src="item.activityProductVO.subPctureUrl || ''" mode="aspectFit"></image>
					</view>
					<view class="info-other">
						<view class="info">
							<view class="label">支付金额</view>
							<view class="value">￥{{ item.tradeAmount }}</view>
						</view>
						<view class="info">
							<view class="label">创建时间</view>
							<view class="value"> {{ item.createTime }}</view>
						</view>
						<view class="sale-btn">
							<button type="default" @click="goToBillDetailPage(item.id, item.payStatus)">查看详情</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="list" v-show="active === 2">
			<view class="item" v-for="item in underway" :key="item.id">
				<view class="title">"{{item.activityProductVO ? item.activityProductVO.name : ''}}"</view>
				<view class="right">支付中</view>
				<view class="infos">
					<view class="info-img">
						<image :src="item.activityProductVO.subPctureUrl || ''" mode="aspectFit"></image>
					</view>
					<view class="info-other">
						<view class="info">
							<view class="label">支付金额</view>
							<view class="value">￥{{ item.tradeAmount }}</view>
						</view>
						<view class="info">
							<view class="label">创建时间</view>
							<view class="value"> {{ item.createTime }}</view>
						</view>
						<view class="sale-btn">
							<button type="default" @click="goToBillDetailPage(item.id, item.payStatus)">查看详情</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="list" v-show="active === 3">
			<view class="item" v-for="item in completed" :key="item.id">
				<view class="title">"{{item.activityProductVO ? item.activityProductVO.name : ''}}"</view>
				<view class="right">已完成</view>
				<view class="infos">
					<view class="info-img">
						<image :src="item.activityProductVO.subPctureUrl || ''" mode="aspectFit"></image>
					</view>
					<view class="info-other">
						<view class="info">
							<view class="label">支付金额</view>
							<view class="value">￥{{ item.tradeAmount }}</view>
						</view>
						<view class="info">
							<view class="label">创建时间</view>
							<view class="value"> {{ item.createTime }}</view>
						</view>
						<view class="sale-btn">
							<button type="default" @click="goToBillDetailPage(item.id, item.payStatus)">查看详情</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<uni-load-more :contentText="contentText" @clickLoadMore="onClickLoadMore" :status="queryStatus">
		</uni-load-more>
	</view>
</template>

<script>
	import {
		getNoSellBox,
		getNoSellWork
	} from '../../request/sale-api.js'

	import {
		getPayOrderPage
	} from '../../request/bill.js'

	export default {
		data() {
			return {
				// 作品类型
				active: 1,
				// 列表数据
				all: [],
				underway: [],
				completed: [],
				// 查询参数
				AllParams: {
					condition: "",
					pageNum: 0,
					pageSize: 10
				},
				UnderwayParams: {
					condition: "PAYMENTS",
					pageNum: 0,
					pageSize: 10
				},
				CompletedParams: {
					condition: "SUCCESS",
					pageNum: 0,
					pageSize: 10
				},
				// 没有更多了
				tab1IsNoMore: false,
				tab2IsNoMore: false,
				tab3IsNoMore: false,
				// 加载状态
				queryStatus: 'more',
				// 各状态文字说明
				contentText: {
					contentdown: "点击加载更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				}
			}
		},
		created() {
			let local = uni.getStorageSync('bill')
			if (!!local) {
				let params = JSON.parse(local)
				this.onTypeChange(params.active)
			} else {
				this.onTypeChange(1)
			}
		},
		methods: {
			// 跳转
			goToBillDetailPage(id, type) {
				this.$tool.navigateTo(`/pages/bill/detail?id=${id}&type=${type}`)
			},

			// 产品切换
			onTypeChange(index) {
				this.active = index
				uni.setStorageSync('bill', JSON.stringify({
					active: index
				}))
				if (index === 1) {
					this.queryStatus = this.tab1IsNoMore ? 'noMore' : 'more'
					if (!this.tab1IsNoMore) {
						if (this.all.length === 0) {
							this.onLoadAllListData()
						}
					}
				} else if (index === 2) {
					this.queryStatus = this.tab2IsNoMore ? 'noMore' : 'more'
					if (!this.tab2IsNoMore) {
						if (this.underway.length === 0) {
							this.onLoadIngListData()
						}
					}
				} else {
					this.queryStatus = this.tab3IsNoMore ? 'noMore' : 'more'
					if (!this.tab3IsNoMore) {
						if (this.completed.length === 0) {
							this.onLoadOverListData()
						}
					}
				}
			},

			// 点击加载更多
			onClickLoadMore() {
				if (this.active === 1) {
					if (!this.tab1IsNoMore) {
						this.onLoadAllListData()
					}
				} else if (this.active === 2) {
					if (!this.tab2IsNoMore) {
						this.onLoadIngListData()
					}
				} else {
					if (!this.tab3IsNoMore) {
						this.onLoadOverListData()
					}
				}
			},

			// 获取全部数据
			async onLoadAllListData() {
				this.queryStatus = 'loading'
				this.AllParams.pageNum += 1
				try {
					let {
						code,
						message,
						data
					} = await getPayOrderPage(this.AllParams)
					if (code === 200) {
						if (this.AllParams.pageNum <= data.totalPage) {
							this.tab1IsNoMore = false
							this.all.push(...data.list)
							this.queryStatus = 'more'
						} else {
							this.tab1IsNoMore = true
							this.queryStatus = 'noMore'
						}
					} else {
						this.queryStatus = 'more'
						uni.showToast({
							title: message,
							icon: 'error'
						})
					}
				} catch (err) {
					this.queryStatus = 'more'
					this.$tool.showErr(err)
				}
			},

			// 获取进行中数据
			async onLoadIngListData() {
				this.queryStatus = 'loading'
				this.UnderwayParams.pageNum += 1
				try {
					let {
						code,
						message,
						data
					} = await getPayOrderPage(this.UnderwayParams)
					if (code === 200) {
						if (this.UnderwayParams.pageNum <= data.totalPage) {
							this.tab2IsNoMore = false
							this.underway.push(...data.list)
							this.queryStatus = 'more'
						} else {
							this.tab2IsNoMore = true
							this.queryStatus = 'noMore'
						}
					} else {
						this.queryStatus = 'more'
						uni.showToast({
							title: message,
							icon: 'error'
						})
					}
				} catch (err) {
					this.queryStatus = 'more'
					this.$tool.showErr(err)
				}
			},

			// 获取已完成数据
			async onLoadOverListData() {
				this.queryStatus = 'loading'
				this.CompletedParams.pageNum += 1
				try {
					let {
						code,
						message,
						data
					} = await getPayOrderPage(this.CompletedParams)
					if (code === 200) {
						if (this.CompletedParams.pageNum <= data.totalPage) {
							this.tab3IsNoMore = false
							this.completed.push(...data.list)
							this.queryStatus = 'more'
						} else {
							this.tab3IsNoMore = true
							this.queryStatus = 'noMore'
						}
					} else {
						this.queryStatus = 'more'
						uni.showToast({
							title: message,
							icon: 'error'
						})
					}
				} catch (err) {
					this.queryStatus = 'more'
					this.$tool.showErr(err)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bill-page {
		width: 100%;
		min-height: 100vh;
		background: $login-background-url no-repeat;
		background-size: 100% auto;
		background-position: 0 -60rpx;
		padding-bottom: 40rpx;
	}

	.list-type {
		width: 100%;
		height: 100rpx;
		padding: 0 40rpx;
		margin: 100rpx 0 60rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.type {
			width: 120rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #8D8D8D;
		}

		.active {
			color: #F7DE75;
		}
	}

	.list {
		width: 100%;
		margin-top: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.item {
			width: 686rpx;
			height: 440rpx;
			padding: 80rpx 40rpx 0rpx 40rpx;
			margin-bottom: 50rpx;
			background: $active-back-url no-repeat;
			background-size: 100% 100%;
			background-position: center center;
			position: relative;

			.right {
				width: 100%;
				height: 40rpx;
				text-align: center;
				font-size: 20rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #FFBD47;
				margin: 10rpx 0;
			}

			.title {
				line-height: 40rpx;
				text-align: center;
				font-size: 14px;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #9D9D9D;
			}

			.infos {
				width: 610rpx;
				max-height: 230rpx;
				padding-top: 10rpx;
				border-top: 4rpx solid rgba(151, 151, 151, 0.2);
				display: flex;
				align-items: center;
				justify-content: space-between;

				.info-img {
					width: 190rpx;

					image {
						width: 190rpx;
						height: 190rpx;
					}
				}

				.info-other {
					flex: 1;
					height: 200rpx;
					max-width: 420rpx;
					max-height: 200rpx;
					padding-left: 20rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-between;

					.info {
						width: 100%;
						height: 48rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;

						.label {
							width: 120rpx;
							font-size: 24rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #979797;
						}

						.value {
							flex: 1;
							max-width: 320rpx;
							text-align: right;
							font-size: 28rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #979797;
						}
					}

					.sale-btn {
						width: 100%;
						padding: 20rpx 0 0 0;
						border-top: 4rpx solid rgba(151, 151, 151, 0.2);
						display: flex;
						justify-content: flex-end;

						button {
							width: 200rpx;
							height: 50rpx;
							margin: 0;
							line-height: 45rpx;
							background: #121212;
							box-shadow: -10px -10px 20px 0px rgba(39, 39, 39, 0.54), 10px 10px 20px 0px rgba(0, 0, 0, 0.83);
							border-radius: 22px;
							border: 1px solid #FFBD47;
							font-size: 14px;
							font-family: SourceHanSansCN-Regular, SourceHanSansCN;
							font-weight: 400;
							color: #FFBD47;
						}
					}
				}
			}

		}
	}
</style>
